from elasticsearch import Elasticsearch, AsyncElasticsearch, helpers

import config
import settings


class ElasticsearchStorage:
    def __init__(self):
        self.client = Elasticsearch([{'host': settings.ES_HOST, 'port': settings.ES_PORT, 'scheme': "http"}],
                                    http_auth=(settings.ES_USER, settings.ES_PASSWORD),
                                    timeout=3600)

    def process(self):
        # 要更新的文档 ID 和索引名称
        doc_id = 'ZY5pEY0BVbxL-jxTj62q'
        index_name = 'dialogues'

        # 获取原始文档
        original_doc = self.client.get(index=index_name, id=doc_id)['_source']

        # 修改数据（示例：将 'field_to_update' 的值更改为新值）
        original_doc['role'] = 'assistant'

        # 创建新的文档
        new_doc = original_doc

        # 重新索引新文档
        self.client.index(index=index_name, id=doc_id, document=new_doc)

        print(f"Document with ID {doc_id} updated successfully.")


if __name__ == '__main__':
    es = ElasticsearchStorage()
    es.process()
